<?php

namespace App\Http\Models\Report;

use App\Http\Models\Business\CachePayerZ1Scale;
use App\Http\Models\CategoryContract;

class ReportPayerZ1Scale
{
    //键名为payer_code, 键值为 Payer 模型
    public $payer_array = [];

    //客户规模基础数据，模型集合
    public $payer_scale;

    private $year = 0;

    //品类模型数组，键名为 品类id,  键值为品类模型
    public $category_contract = [];

    public function __construct(int $year, int $payer_code)
    {
        $this->year = $year;
        $this->setPayer($payer_code);
        $this->setPayerScale($payer_code);
        $this->setCategory();
        $this->assign_data();
    }


    private function setCategory()
    {
        $category = CategoryContract::all();
        foreach ($category  as $value) {
            $this->category_contract[$value->id] = $value;
        }
    }


    private function setPayerScale(int $payer_code)
    {
        $where['year'] =  $this->year;
        if ($payer_code != 0) {
            $where['payer_code'] =  $payer_code;
        }
        $this->payer_scale = CachePayerZ1Scale::where($where)->get();
    }


    private function setPayer(int $payer_code)
    {
        $where = [];
        if ($payer_code != 0) {
            $where['code'] = $payer_code;
        }
        $payers = PayerReportPayerZ1Scale::where($where)->get();
        foreach ($payers as $payer) {
            $this->payer_array[$payer->code] = $payer;
        }
    }


    private function assign_data()
    {
        foreach ($this->payer_scale as $value) {
            $this->payer_array[$value->payer_code]->addPayerScale($value);
        }

        foreach ($this->payer_array as $payer) {
            $payer->setPayerZ1ScaleResultForSearch();
        }
    }
}
